package org.hhchat.leetcode.hashtable;

import java.util.Arrays;
import java.util.HashMap;

public class code290_easy {
    static class Solution {
        public boolean wordPattern(String pattern, String str) {
            int postion[] = new int[256];
            HashMap<String, Integer> wordPosition = new HashMap<>();
            String[] words = str.split("\\s");
            if (words.length != pattern.length()) {
                return false;
            }
            Arrays.fill(postion,-1);
            for (String word : words) {
                wordPosition.put(word, -1);
            }
            for (int i = 0; i < pattern.length(); i++) {
                if (postion[pattern.charAt(i)] != wordPosition.get(words[i])) {
                    return false;
                }
                wordPosition.put(words[i], i);
                postion[pattern.charAt(i)] = i;
            }
            return true;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.wordPattern("abba","dog cat cat fish"));
    }
}
